<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GMM/SM 业务查询</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.4.4/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.4.4/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

var page=0;
var lasttime;
function searchGPRS_full(){
	page=1;
	
	var data = getCommontData();
	if(data.startTime - data.endTime > 0) {
		$.messager.alert("系统提示","查询开始时间"+data.startTime+"不能大于结束时间"+data.endTime+"！");
		return;
	}
	
	sendData(data, page, '1');
	$("#detailInfo").html('');
}

function sendData(data, page, increase) {
	var currentpage = "第 "+page+" 页";
	var p=$("#dg").datagrid("getPager")
	$(p).pagination({ //设置分页页数
		displayMsg:currentpage
	});
	$("#dg").datagrid('load',{
		"startTime":data.startTime,
		"endTime":data.endTime,
		"IMSI":data.IMSI,
		"MSISDN":data.MSISDN,
		"gEngineNo":data.gEngineNo,
		"gTrainNo":data.gTrainNo,
		"MessageType":data.MessageType,
		"TLLI":data.TLLI,
		"search":'1',
		"increase":increase,
		"destIP": data.destIP,
		"gServiceType" : data.gServiceType,
		"recordId": data.recordId,
		"sourceIP": data.sourceIP,
		"BSName": data.BSName
	});
}


function getCommontData() {
	var IMSI = '';
	var MSISDN = '';
	var gEngineNo = '';
	
	if ($('#in1').searchbox('getName') == 'IMSI') {
		IMSI = $('#in1').searchbox('getValue');
	} else if ($('#in1').searchbox('getName') == 'MSISDN') {
		MSISDN = $('#in1').searchbox('getValue');
	} else if ($('#in1').searchbox('getName') == 'CTCS_ID') {
		gEngineNo = $('#in1').searchbox('getValue');
	}
	
	var gTrainNo = $("#gTrainNo").val();
	var MessageType = $("#messageType1").combotree("getText");
	var resultType = MessageType.split(',');
	var MessageID = $("#messageType1").combotree("getValues");
	
	var resultId = [];
	for (var i = MessageID.length; i >= 0; i--) {
		if (MessageID[i] < 1000) {
			resultType.splice(i, 1);
			resultId.push(MessageID[i]);
		}
	}
	MessageType = resultType.join();
	var gServiceType = resultId.join();

	var TLLI = $("#TLLI").val();
	var tlli = TLLI.length > 0 ? parseInt(TLLI,16):'';
	var recordId = $("#recordId").val();
	var destIP = $("#destIP").val();
	var sourceIP = $("#sourceIP").val();
	var BSName = $("#BSName").combotree("getText");;
	var startDate = new Date($("#i1").datebox("getValue").replace(new RegExp("-","gm"),"/"));
	var endDate = new Date($("#i2").datebox("getValue").replace(new RegExp("-","gm"),"/"));
	var startTimeLong = startDate.getTime();
	var endTimeLong = endDate.getTime();
	
	return {"startTime":startTimeLong, "endTime":endTimeLong, "IMSI":IMSI, "MSISDN":MSISDN, "gEngineNo":gEngineNo, "gTrainNo":gTrainNo, "BSName": BSName,
		"MessageType":MessageType, "TLLI":tlli, "destIP": destIP, "gServiceType" : gServiceType, "sourceIP": sourceIP, "recordId": recordId};
}

function searchNext(){
	var size = $("#dg").datagrid("getPager" ).data("pagination" ).options.pageSize;
	var rows = $('#dg').datagrid("getRows");  
	if(rows.length<size){
		$.messager.alert("系统提示","当前页已经是最后一页！");
		return;
	} else {
		lasttime=rows[rows.length-1].unixTimeStamp;
	}
	
	var data = getCommontData();
	
	data.startTime = lasttime;
	page=page+1;
	sendData(data, page, '1');
}

function searchBack(){
	if(page==1){
		$.messager.alert("系统提示","当前页已经是第一页！");
		 return;
	}
	
	var data = getCommontData();
	
	var rowsbefore = $('#dg').datagrid("getRows"); 
	if(rowsbefore.length > 0){
		data.endTime = rowsbefore[0].unixTimeStamp;
	} else {
		data.endTime = lasttime;
	}
	page=page-1;
	
	sendData(data, page, '0');
}
	
	
	function formatDatebox(value) {
	 	var datetimeType = "";
	    var date =new Date(parseInt(value));
	    datetimeType = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
	    +" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+" "+date.getMilliseconds();
	    return datetimeType;
	}
	
	function formatGData(value, row) {
		if (value.length < 3) return row.decoded;
		return JSON.stringify(value);
	}
	
	function formatGOrder(value) {
		if (value == '1') {
			return 'GPRS终端活动性检测';
		} else if (value == '2') {
			return 'GPRS终端活动性检测响应';
		} else if (value == '32') {
			return '列车进路预告';
		} else if (value == '51') {
			return '调度命令自动确认';
		} else if (value == '53') {
			return '调度命令手动签收';
		}
		return value;
	}
	
	function formatGFunctionCode(value) {
		if (value == '1') {
			return '调度命令';
		} else if (value == '2') {
			return '路票';
		} else if (value == '3') {
			return '绿色许可证';
		} else if (value == '4') {
			return '红色许可证';
		} else if (value == '5') {
			return '出站跟踪调车通知书';
		} else if (value == '17') {
			return '调车作业通知单';
		} else if (value == '18') {
			return '调车请求确认';
		} else if (value == '32') {
			return '出入库检查';
		} else if (value == '7') {
			return '列车进路预告信息';
		} else if (value >= '24' && value <='31') {
			return '其它消息';
		} else {
			return value;
		}
	}
	
	function formatGServiceType(value) {
		if(value==5){
			return "列车无线车次号校核信息";
		}else if(value==6){
			return "调度命令信息";
		}else if(value==7){
			return "列车停稳/启动信息";
		}else if(value==14){
			return "M-GRIS IP地址查询/更新/响应信息";
		}else if(value==15){
			return "GRIS IP地址查询/更新/响应信息";
		}else if(value==34){
			return "站场车号商检信息";
		}else if(value==36){
			return "站场调车作业单信息";
		}else if(value==241){
			return "GPRS终端活动性检测信息";
		}else if(value==18){
			return "TCDS信息";
		}else if(value==48){
			return "DMS信息";
		}else if(value==160){
			return "机车信号远程监控信息";
		}else if(value==10){
			return "工务晃车信息";
		}else if(value==12){
			return "RMD信息";
		}else if(value==4){
			return "列尾风压信息";
		}else if(value==19){
			return "GIR出入库检测信息";
		}else {
			return value;
		}
	}
	
	function postExcelFile(params, url) { //params是post请求需要的参数，url是请求url地址
	    var form = document.createElement("form");
	    form.style.display = 'none';
	    form.action = url;
	    form.method = "post";
	    document.body.appendChild(form);

	    for(var key in params){
	      var input = document.createElement("input");
	      input.type = "hidden";
	      input.name = key;
	      input.value = params[key];
	      form.appendChild(input);
	    }
	    form.submit();
	    form.remove();
	  }
	
	function exportGprs_fullInCurrentPage(){
		var rows  = $('#dg').datagrid("getRows");
		if (rows.length < 1) {
			$.messager.alert("系统提示", "请查询需要导出的数据");
			return;
		}
		/*
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].gData.lenght > 2) continue;
			rows[i].gData = rows[i].decoded;
		}
		console.log(rows[0]);
		*/
    	var params = {data:JSON.stringify(rows)};
    	console.log(params);
    	postExcelFile(params, "${pageContext.request.contextPath}/gprs_full/exportcurrentpage.do");
	}
	function exportByTemplateInCurrentPage(){
		var rows = $("#dg").datagrid("getRows");
		var strIds=[];
		 for(var i=0;i<rows.length;i++){
			 strIds.push(rows[i].id);
		 }
		 var ids=strIds.join(",");
		 window.open('${pageContext.request.contextPath}/gprs_full/exportByTemplate.do?ids='+ids+'');
	}
	
	function exportGprs_fullInAllPages(){

		var data = getCommontData();
		window.open('${pageContext.request.contextPath}/gprs_full/export.do?startTime='+data.startTime+'&endTime='+data.endTime+'&IMSI='+data.IMSI+'&MSISDN='+data.MSISDN+
				'&gEngineNo='+data.gEngineNo+'&gTrainNo='+data.gTrainNo+'&MessageType='+data.MessageType+'&TLLI='+data.TLLI+'&gServiceType='+data.gServiceType+'&BSName='+data.BSName+'&destIP='+data.destIP+'&sourceIP='+data.sourceIP+'&recordId='+data.recordId);
	}
	
	function exportByTemplateInAllPages(){
		 window.open('${pageContext.request.contextPath}/gprs_full/exportByTemplate.do?ids='+null+'');
	}
	//重写分页
	$(function(){
		
		$('#dg').datagrid({
	    	onClickRow: function(index,row){
				$("#detailInfo").html('');
	    		if (row == null || row.gData.length < 3) {
	    			$("#detailInfo").html(row.decoded);
					return;
	    		}
	    		var gData = JSON.stringify(row.gData);
	    		console.log(row);
	    		var result = '';
	    		if (row.gOrder == 32) {
	    			var result = gData.replace(/\\",\\"/g, "\n").replace(/\\":\\"/g, "：").replace(/,\\"/g, "\n").replace(/\\":/g, "：").replace('"{\\"', '').replace('\\"}"', '').replace(/\\r\\n/g, ', ');
	    		} else {
	    			var result = gData.replace(/,"/g, "\n").replace(/":/g, '：').replace("{\"", "").replace("}", "");
	    		}
	    		
	    		$("#detailInfo").html(result);
	    		console.log(gData);
	    	}
	    }); 
		
		var p = $('#dg').datagrid('getPager');
	    $(p).pagination({ //设置分页功能栏
	           //分页功能可以通过Pagination的事件调用后台分页功能来实现
	            //layout:['prev','next'],
                layout:['list','sep'],
                /* beforePageText: '第',//页数文本框前显示的汉字 
                afterPageText: '页', */
	            displayMsg:"",   
	            buttons:[{
	    			iconCls:'icon-undo', handler:function(){ searchGPRS_full(); }
	    		},{
	    			iconCls:'icon-back', handler:function(){ searchBack(); }
	    		}, {
	    			iconCls:'icon-next', handler:function(){ searchNext(); }
	    		}]
	    });
	    
	    $('#messageType1').combotree('loadData', [
	    	{id:'1000', text:'IP package', "state":"closed", children: [
		    	{id: 5, text: '列车无线车次号校核信息'},
		    	{id: 6, text: '调度命令信息'},
		    	{id: 7, text: '列车停稳/启动信息'},
		    	{id: 14, text: 'M-GRIS IP地址查询/更新/响应信息'},
		    	{id: 15, text: 'GRIS IP地址查询/更新/响应信息'},
		    	{id: 34, text: '站场车号商检信息'},
		    	{id: 36, text: '站场调车作业单信息'},
		    	{id: 241, text: 'GPRS终端活动性检测信息'},
		    	{id: 18, text: 'TCDS信息'},
		    	{id: 48, text: 'DMS信息'},
		    	{id: 160, text: '机车信号远程监控信息'},
		    	{id: 10, text: '工务晃车信息'},
		    	{id: 12, text: 'RMD信息'},
		    	{id: 4, text: '列尾风压信息'},
		    	{id: 19, text: 'GIR出入库检测信息'}]},
		    {id:'1001', text:'Authentication And Ciphering Req'},
		    {id:'1002', text:'Attach Accept'},
		    {id:'1006', text:'Activate PDP context reject'},
		    {id:'1007', text:'Authentication And Ciphering Resp'},
		    {id:'1008', text:'Attach Comelete'},
		    {id:'1009', text:'Activate PDP context request'},
		    {id:'1010', text:'Attach Request'},
		    {id:'1004', text:'Activate PDP context accept'},
		    {id:'1003', text:'Deactivate PDP context accept'},
		    {id:'1005', text:'Deactivate PDP context request'},
		    {id:'1014', text:'Detach Accept'},
		    {id:'1011', text:'Unknown GMM Message Type'},
		    {id:'1012', text:'Identity Response'},
		    {id:'1013', text:'Identity Request'},
		    {id:'1015', text:'Routing Area Update Request'},
		    {id:'1016', text:'Routing Area Update Complete'},
		    {id:'1017', text:'Routing Area Update Accept'}]);
	    
	    $('#BSName').combotree({
	    	url: '${pageContext.request.contextPath}/sdcch/sdcchDistrictComboList.do'
	    });
	    
	    $('#i1').datetimebox('setValue', formatterDate(new Date(new Date(new Date().toLocaleDateString()).getTime())));
		$('#i2').datetimebox('setValue', formatterDate(new Date()));
		$('.searchbox-button').remove();
	});
	
	formatterDate = function (date) {
		var day = date.getDate();
		var month = (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
	};
</script>
</head>
<body style="margin: 1px">
<table id="dg" title="GMM/SM 业务查询" class="easyui-datagrid"
   fitColumns="false" pagination="true" rownumbers="true"  pageSize="100" pageList="[100,200,300,400,500]"
   url="${pageContext.request.contextPath}/gprs_full/list.do" fit="true" toolbar="#tb" singleSelect="true">
   <thead>
   	<tr>
   		<th field="cb" checkbox="true" align="center"></th>
   		<th field="startTime" width="140" align="center">触发时间</th>
   		<th field="IMSI" width="80" align="center">IMSI</th>
   		<th field="TLLI" width="90" align="center">TLLI</th>
   		<th field="gTrainNo" width="80" align="center">车次号</th>
   		<th field="gEngineNo" width="80" align="center">机车号</th>
   		<th field="BSName" width="80" align="center">小区名称</th>
   		<th field="BCCH" width="80" align="center">BCCH</th>
   		<th field="TCH" width="80" align="center">TCH</th>
   		<th field="timeSlot" width="80" align="center">时隙号</th>
		<th field="USF" width="80" align="center">USF</th>
		<th field="TFI" width="80" align="center">TFI</th>
   		<th field="frameNumber" width="80" align="center">帧号</th>
   		<th field="direction" width="80" align="center">方向</th>
   		<th field="messageType" width="120" align="center">Um接口消息类型</th>
   		<th field="dataLayer" width="80" align="center">DataLayer</th> 
   		<th field="sourceIP" width="100" align="center">源IP地址</th>
   		<th field="destinationIP" width="100" align="center">目的IP地址</th>
   		<th field="gSourceIP" width="100" align="center">源IP地址（包）</th>
   		<th field="gDstIP" width="100" align="center">目的IP地址（包）</th>
   		<th field="gServiceType" width="80" align="center" formatter="formatGServiceType">业务类型</th>
   		<th field="gOrder" width="80" align="center" formatter="formatGOrder">功能码</th>
   		<th field="gData" width="70" align="center" formatter="formatGData">解码信息</th>
   		<th field="MSISDN" width="80" align="center">MSISDN</th>
   		<!-- <th field="frameNumber" width="80" align="center">FrameNumber</th> -->
		<th field="LEVEL" width="120" align="center">LEVEL</th>
		<th field="SNR" width="150" align="center">SNR</th>
		<th field="QUALITY" width="120" align="center">QUALITY</th>
   		<th field="gDispatchCode" width="80" align="center">调度命令</th>
   		<th field="dataLength" width="70" align="center">数据长度</th>
   		<th field="sourceCode" width="140" align="center">数据源码</th>
   		<!-- <th field="decoded" width="80" align="center">decoded</th> -->
   		<!-- <th field="gSrcPortFrame" width="80" align="center">gSrcPortFrame</th> -->
   		<!-- <th field="gDstPortFrame" width="80" align="center">gDstPortFrame</th> -->
   		<th field="LAC" width="80" align="center">LAC</th>
   		<th field="CI" width="80" align="center">CI</th>
   		<th field="frontProcessorID" width="80" align="center">前置机号</th>
   		<th field="fileNo" width="80" align="center">记录号</th>
   		<!-- <th field="gGoodCRC" width="80" align="center">gGoodCRC</th> -->
   		<!-- <th field="gkmMark" width="80" align="center">gkmMark</th>
   		<th field="gDataDirection" width="80" align="center">gDataDirection</th> -->
   	</tr>
   </thead>
 </table>
 <div id="tb">
 	<div style="margin:3px">
 		<a href="javascript:exportGprs_fullInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出当前页GMM/SM/业务</a>
 		<a href="javascript:exportGprs_fullInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出所有页GMM/SM/业务</a>
 		<!-- <a href="javascript:exportByTemplateInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出当前GMM/SM/业务</a>
 		<a href="javascript:exportByTemplateInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出所有GMM/SM/业务</a> -->
 	</div>
 	
 	<div style="margin:3px">
 		&nbsp;&nbsp;小区：<select id="BSName" class="easyui-combotree" style="width:180px;" data-options="panelHeight:'auto', panelMaxHeight:'240px'" multiple></select>
 		&nbsp;&nbsp;时间：<input type="text" id="i1" style="width:calc(8%)" class="easyui-datetimebox"  />
 		&nbsp;至&nbsp;
		<input type="text" id="i2" style="width:calc(8%)" class="easyui-datetimebox"/>
		&nbsp;&nbsp;车次号：<input type="text" style="width:calc(5%)" id="gTrainNo" name="gTrainNo"  onkeydown="if(event.keyCode==13) searchGPRS_full()"/>
		&nbsp;&nbsp;Um接口消息类型：<select id="messageType1" class="easyui-combotree" style="width:280px;" data-options="panelHeight:'auto', panelMaxHeight:'240px'" multiple></select>&nbsp;&nbsp;
 		&nbsp;&nbsp;TLLI：<input type="text" style="width:calc(5%)" id="TLLI" name="TLLI" onkeydown="if(event.keyCode==13) searchGPRS_full()"/>
 		&nbsp;&nbsp;源IP地址 ：<input type="text" style="width:calc(5%)" id="sourceIP" name="sourceIP" onkeydown="if(event.keyCode==13) searchGPRS_full()"/>
 		&nbsp;&nbsp;目的IP地址 ：<input type="text" style="width:calc(5%)" id="destIP" name="destIP" onkeydown="if(event.keyCode==13) searchGPRS_full()"/>
 		&nbsp;&nbsp;记录号 ：<input type="text" style="width:calc(5%)" id="recordId" name="recordId" onkeydown="if(event.keyCode==13) searchGPRS_full()"/>
 		
 		<br>
 		&nbsp;&nbsp;<input id="in1" class="easyui-searchbox" data-options="prompt:'Please Input Value',menu:'#mm'" style="width:260px; margin-top:-20px"></input>
		<div id="mm">
			<div data-options="name:'IMSI'">IMSI</div>
			<div data-options="name:'MSISDN'">MSISDN</div>
			<div data-options="name:'CTCS_ID'">机车号</div>
		</div>
		<a href="javascript:searchGPRS_full()" class="easyui-linkbutton" iconCls="icon-search" plain="true"><b style="font-size:15px">查询</b></a>
		
		
 	</div>
 	<textarea id="detailInfo" style="width:99%; height:100px; margin:4px"></textarea>
 </div>
 
</body>
</html>